/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package reyes.mysite.struts;

import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

import reyes.mysite.jdo.Blog;
import reyes.mysite.dao.GenericDAO;

/**
 *
 * @author reyesyang
 */
public class PostNewBlogAction extends org.apache.struts.action.Action {

    @Override
    public ActionForward execute(
            ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response)
            throws Exception {

        PostNewBlogActionForm blogForm = (PostNewBlogActionForm) form;
        
        String blogTitle = blogForm.getBlogTitle();
        String blogCategory = blogForm.getBlogCategory();
        String blogPrivacy = blogForm.getBlogPrivacy();
        String blogSummary = blogForm.getBlogSummary();
        String blogContent = blogForm.getBlogContent();
        Date blogDate = new Date();

        GenericDAO<Blog> blogDAO = new GenericDAO<Blog>();

        try {
            //Key key = KeyFactory.createKey(Blog.class.getSimpleName(), 123);
            Blog blog = new Blog(blogTitle, blogCategory, blogPrivacy, blogDate, blogSummary, blogContent);
            blogDAO.save(blog);
            request.setAttribute("blog", blog);
        } catch (Exception e) {
            System.out.println("Post new blog failure, Details: "+e.getMessage());
        }finally{
            blogDAO.closePM();
        }

        return mapping.findForward("adminBlogDetails");
    }
}
